using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dillon.CSV
{
    //class Program
    //{
    //    static bool printTableValue = false;
    //    static void Main(string[] args)
    //    {
    //        string line;
    //        while ((line = Console.ReadLine()) != "Exit")
    //        {
    //            switch (line.ToUpper())
    //            {
    //                case "O":
    //                case "OPEN":
    //                    printTableValue = true;
    //                    continue;
    //                case "C":
    //                case "CLOSE":
    //                    printTableValue = false;
    //                    continue;
    //                default:
    //                    break;
    //            }

    //            var value = line;
    //            if (System.IO.File.Exists(value))
    //            {
    //                using (var sr = new System.IO.StreamReader(value, Encoding.Default))
    //                    value = sr.ReadToEnd();
    //            }
    //            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    //            sw.Start();
    //            var result = Parse(value);
    //            sw.Stop();
    //            if (printTableValue)
    //            {
    //                PrintTableValue(result);
    //            }
    //            Console.WriteLine();
    //            Console.WriteLine(string.Format("解析時間：{0}  {1} 件", sw.Elapsed, result.Count));
    //        }

    //    }

    //    static IList<IList<string>> Parse(string value)
    //    {
    //        TableValueLexer lexer = new TableValueLexer(new StringReader(value), new char[] { ',', '\t' });
    //        IParser parser = new Parser(lexer);

    //        return parser.ParseTableValue();
    //    }

    //    static void PrintTableValue(IList<IList<string>> tableValues)
    //    {
    //        var maxColumnCount = GetMaxCount(tableValues);
    //        for (int i = 0; i < tableValues.Count; i++)
    //        {
    //            if (i > 0)
    //            {
    //                Console.WriteLine();
    //            }
    //            Console.Write(string.Format("第{0}行　", (i + 1).ToString("D" + tableValues.Count.ToString().Length)));
    //            Console.Write(string.Format("コラム数：{0}", (tableValues[i].Count).ToString("D" + maxColumnCount.ToString().Length)));

    //            Console.Write(">>>>");
    //            PrintRowValues(tableValues[i]);
    //        }

    //    }

    //    static int GetMaxCount(IList<IList<string>> tableValues)
    //    {
    //        int count = 0;
    //        foreach (var item in tableValues)
    //        {
    //            count = Math.Max(count, item.Count);
    //        }
    //        return count;
    //    }

    //    static void PrintRowValues(IList<string> rowValules)
    //    {
    //        for (int i = 0; i < rowValules.Count; i++)
    //        {
    //            if (i > 0)
    //            {
    //                Console.Write(" ┃ ");
    //            }

    //            foreach (var item in rowValules[i])
    //            {
    //                switch (item)
    //                {
    //                    case '\t':
    //                        Console.Write(@"\t");
    //                        break;
    //                    case '\r':
    //                        Console.Write(@"\r");
    //                        break;
    //                    case '\n':
    //                        Console.Write(@"\n");
    //                        break;
    //                    default:
    //                        Console.Write(item);
    //                        break;
    //                }

    //            }
    //        }
    //    }

    //    static int GetColumnMaxCharCount(IList<IList<string>> tableValues, int columnIndex)
    //    {
    //        int columnMaxCharCount = 0;
    //        foreach (var item in tableValues)
    //        {
    //            if (item.Count <= columnIndex)
    //            {
    //                continue;
    //            }
    //            columnMaxCharCount = Math.Max(columnMaxCharCount, item[columnIndex].Length);
    //        }
    //        return columnMaxCharCount;
    //    }

    //}

                    if (pageIndex <= 1)
                    {
                        listToSort = listToSort.AsEnumerable().Take(pageSize).ToList();
                    }
                    else
                    {
                        listToSort = listToSort.AsEnumerable().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                    }


                     for (int m = lstFdRep.Count - 1; m >= 0; m--)
            {
                if (lstFdRep[m].RepaymentType.Equals(RepaymentType.TenderReceive))
                    lstFdRep.RemoveAt(m);
            }


            int batchMaxCount = FundsDepositConfig.Request_OperaMaxCount == null ? 100 : int.Parse(FundsDepositConfig.Request_OperaMaxCount.ToString());
            //收款资金从大到小排序
            lstBizFDRepayMentRecords = lstBizFDRepayMentRecords.OrderByDescending(r => r.TransferAmount).ToList();
            for (int m = 0; m < lstBizFDRepayMentRecords.Count; m++)
            {
                intReceRecordsCount = m;
                lstBizFDRepayMentRecords[m].BFFromUserid = mode.strBFjkUserid;
                lstBizFDRepayMentRecords[m].BFToUserid = DicUsers[lstBizFDRepayMentRecords[m].ToUserid];
                lstBizFDRepayMentRecords[m].DJOrderId = strHKFreeingOrderId;
                strHKBatchNo = intReceRecordsCount % batchMaxCount == 0 ? ComFunc.GetBatchNo(BatchType.ReceiveBatchId) : strHKBatchNo;
                strUuid = intReceRecordsCount % batchMaxCount == 0 ? Guid.NewGuid().ToString() : strUuid;
                decimal decTransferAmount = lstBizFDRepayMentRecords[m].TransferAmount.Value;
                lstBizFDRepayMentRecords[m].TransferOutServiceFee = 0.0m;
                lstBizFDRepayMentRecords[m].Uuid = strUuid;
                if (!string.IsNullOrEmpty(lstBizFDRepayMentRecords[m].BFToUserid) && isFirst)
                {
                    if (decServiceFee > 0)
                        lstBizFDRepayMentRecords[m].TransferOutServiceFee = decServiceFee;//手续费的集合集体一次性给公司（还款在第一个人的时候）

                    if (decCompanyPay > 0)//差额部分公司一次性给出
                    {
                        decTransferAmount = lstBizFDRepayMentRecords[m].TransferAmount.Value - decCompanyPay;
                        //在手续费中加一条公司支出差额的记录
                        TransferBaseData TransferCompanyPay = new TransferBaseData();
                        TransferCompanyPay.Crje = decCompanyPay;
                        TransferCompanyPay.Ftype = RelatedType.CompanyPay;
                        TransferCompanyPay.IsKh = true;
                        TransferCompanyPay.Userid = lstBizFDRepayMentRecords[m].ToUserid;
                        lstServiceFeeTranData.Add(TransferCompanyPay);
                    }
                    //为了防止精度差问题将还款金额扣成负值
                    lstBizFDRepayMentRecords[m].TransferLX = decTransferAmount - lstBizFDRepayMentRecords[m].TransferBJ;
                    if (lstBizFDRepayMentRecords[m].TransferLX < 0)
                        lstBizFDRepayMentRecords[m].TransferLX = 0;
                    //lstBizFDRepayMentRecords[m].TransferBJ = decTransferAmount - lstBizFDRepayMentRecords[m].TransferLX;
                    isFirst = false;
                }
                lstBizFDRepayMentRecords[m].TransferAmount = decTransferAmount;
                lstBizFDRepayMentRecords[m].BatchNo = strHKBatchNo;

                if (!string.IsNullOrEmpty(lstBizFDRepayMentRecords[m].BFToUserid))
                {
                    lstBizFDRepayMentRecords[m].IsRequestedInBF = true;
                    lstBizFDRepayMentRecords[m].TransferStatus = TransferStatus.AcceptedType;
                }
                else
                {
                    lstBizFDRepayMentRecords[m].IsRequestedInBF = false;
                    lstBizFDRepayMentRecords[m].TransferStatus = TransferStatus.SuccessType;
                }
                lstFdRep.Add(lstBizFDRepayMentRecords[m]);
                intReceRecordsCount++;
            }


            List<BizFDRepaymentTransferRecords> lstBizReceiveRep = lstFdRep.Where(f => f.RepaymentType.Equals(RepaymentType.TenderReceive) && f.IsRequestedInBF == true).ToList();
                var receiveBatchRecords = lstBizReceiveRep.GroupBy(p => p.BatchNo).Select(group => new { BatchNo = group.Key, FDRecords = group.AsEnumerable() });
                foreach (var receReocrd in receiveBatchRecords)
                {
                    List<BizFDRepaymentTransferRecords> lstBatchFdReceRecord = receReocrd.FDRecords.Cast<BizFDRepaymentTransferRecords>().ToList();

                    repaymentBiz.HsRepayMentTenderReceive(lstBatchFdReceRecord);
                }

                if (fdRecordMBLst.Count > 0)
                {
                    //调用满标接口
                    var batchNoRecords = fdRecordMBLst.GroupBy(p => p.BatchNo).Select(group => new { BatchNo = group.Key, FDRecords = group.AsEnumerable() }).OrderBy(m => m.BatchNo);
                    int passCount = 0;
                    foreach (var mbRecord in batchNoRecords)
                    {
                        List<BizFundsDepositTransferRecords> lstBatchFdMBRecord = mbRecord.FDRecords.Cast<BizFundsDepositTransferRecords>().ToList();
                        string memo = SendMBApply(loan, lstBatchFdMBRecord, lstTenderinfo);
                        if (memo.Equals("满标请求已经成功发送，请稍后查询满标情况！"))
                            passCount++;
                    }

                    if (passCount == batchNoRecords.Count())
                        SuccessStatus = "localAndBF";
                }
}
